//
//  CQTemplateRectHelper.m
//  TSDemo_ImageFilter
//
//  Created by ciyouzen on 2014/5/8.
//  Copyright (c) 2014年 dvlproad. All rights reserved.
//

#import "CQTemplateRectHelper.h"
#import "CJTemplateRectHelper.h"

@implementation CQTemplateRectHelper

/*
 *  获取模型在真正坐标系下的真实坐标等信息
 *
 *  @param subViewCount             样式的子框个数
 *  @param styleIndex               对应子框个数下的样式类型
 *  @param templateRealSuperFrame   模型所在的真正坐标系
 *
 *  @return 模型在真正坐标系下的真实坐标等信息
 */
+ (CJTemplateModel *)getRealTemplateModelsWithSubViewCount:(NSInteger)subViewCount
                                                styleIndex:(NSInteger)styleIndex
                                  inTemplateRealSuperFrame:(CGRect)templateRealSuperFrame
{
    NSDictionary *templateDictionary = [self templateDictionaryForSubViewCount:subViewCount styleIndex:styleIndex];
    
    CJTemplateModel *templateModel = [[CJTemplateModel alloc] init];
    
    NSMutableArray<CJTemplatePositionModel *> *realTemplateModels = [self getRealTemplateModelsWithTemplateDictionary:templateDictionary inTemplateRealSuperFrame:templateRealSuperFrame];
    templateModel.realTemplateModels = realTemplateModels;
    
    NSString *templateTitle = templateDictionary[@"title"];
    templateModel.title = templateTitle;
    
    return templateModel;
}

/*
 *  获取样式信息
 *
 *  @param subViewCount     样式的子框个数（最小为1）
 *  @param styleIndex       对应子框个数下的样式类型
 *
 *  @return 样式信息(含样式的frame和其上的各个point)
 */
+ (NSDictionary *)templateDictionaryForSubViewCount:(NSInteger)subViewCount styleIndex:(NSInteger)styleIndex {
    if (subViewCount < 1) {
        return nil;
    }
    
    NSString *picCountFlag = @"";
    switch (subViewCount) {
        case 1:
            picCountFlag = @"one";
            break;
        case 2:
            picCountFlag = @"two";
            break;
        case 3:
            picCountFlag = @"three";
            break;
        case 4:
            picCountFlag = @"four";
            break;
        case 5:
            picCountFlag = @"five";
            break;
        default:
            break;
    }
    
    NSDictionary *templateDictionary = nil;
    if (![picCountFlag isEqualToString:@""]) {
        //NSBundle *podBundle = [NSBundle mainBundle];
        NSBundle *bundle = [NSBundle bundleForClass:[self class]];
        NSURL *url = [bundle URLForResource:@"CQSplicedTemplateKit" withExtension:@"bundle"];
        NSBundle *podBundle = [NSBundle bundleWithURL:url];
        
        NSString *styleFileName = [NSString stringWithFormat:@"number_%@_style_%d.plist", picCountFlag, styleIndex];
        NSString *file = [podBundle.resourcePath stringByAppendingPathComponent:styleFileName];
        templateDictionary = [NSDictionary dictionaryWithContentsOfFile:file];
    }
    
    return templateDictionary;
}

/*
 *  获取模型在真正坐标系下的真实坐标等信息
 *
 *  @param templateDictionary       模型的字典信息
 *  @param templateRealSuperFrame   模型所在的真正坐标系
 *
 *  @return 模型在真正坐标系下的真实坐标等信息
 */
+ (NSMutableArray<CJTemplatePositionModel *> *)getRealTemplateModelsWithTemplateDictionary:(nullable NSDictionary *)templateDictionary                                                               inTemplateRealSuperFrame:(CGRect)templateRealSuperFrame
{
    NSMutableArray<CJTemplatePositionModel *> *realTemplateModels = [[NSMutableArray alloc] init];
    if (templateDictionary) {
        CGSize orginTemplateSuperSize = CGSizeFromString([[templateDictionary objectForKey:@"SuperViewInfo"] objectForKey:@"size"]);
        NSArray *subViewArray = [templateDictionary objectForKey:@"SubViewArray"];
        for(int i = 0; i < subViewArray.count; i++) {
            NSDictionary *subDict = [subViewArray objectAtIndex:i];
            NSArray *templatePointArray = [subDict objectForKey:@"pointArray"];
            CJTemplatePositionModel *realTemplateModel =
                [CJTemplateRectHelper realTemplateModelWithTemplatePointStrings:templatePointArray
                                                               fullTemplateSize:orginTemplateSuperSize
                                                            mapToRealSuperFrame:templateRealSuperFrame];
            
            [realTemplateModels addObject:realTemplateModel];
        }
    }
    
    return realTemplateModels;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
